package inits

import (
	"driver/cmd/basic/config"
	"driver/internal/biz"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func MysqlInit() {
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	//dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
	datamy := config.Config.MySQL
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		datamy.User,
		datamy.Password,
		datamy.Host,
		datamy.Port,
		datamy.Database,
	)
	config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err.Error())
	}
	fmt.Println("mysql连接成功")
	err = config.DB.AutoMigrate(&biz.DriverVehicle{}, &biz.VehicleUser{})
	if err != nil {
		panic(err)
	}
	fmt.Println("迁移成功")
}
